package com.xiang.reflection;


import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

//通过反射获取泛型
public class test10 {

    public void test01(Map<String,User> map, List<User> list){
        System.out.println("test01");
    }

    public Map<String,User> test02(){
        System.out.println("test02");
        return null;
    }

    public static void main(String[] args) throws NoSuchMethodException {
        Method test01 = test10.class.getMethod("test01", Map.class, List.class);

        //泛型，获取泛型的类型
        Type[] genericParameterTypes = test01.getGenericParameterTypes();


        for (Type genericParameterType : genericParameterTypes) {
            System.out.println("#"+genericParameterTypes);
            //instansof，属于结构化参数类型,如果是结构化参数类型，就解析出来并打印
            if (genericParameterType instanceof ParameterizedType){

                //强转，解析出其中的真实的参数类型信息
                Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();

                for (Type actualTypeArgument : actualTypeArguments) {
                    System.out.println(actualTypeArgument);
                }
            }
        }

        System.out.println("=======================================================================================================================================================");
        Method test02 = test10.class.getMethod("test02", null);
        Type genericReturnType = test02.getGenericReturnType();
        System.out.println(genericReturnType);
        //instansof，属于结构化参数类型,如果是结构化参数类型，就解析出来并打印
        if (genericReturnType instanceof ParameterizedType){

            //强转，解析出其中的真实的参数类型信息
            Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();

            for (Type actualTypeArgument : actualTypeArguments) {
                System.out.println(actualTypeArgument);
            }
        }
    }
}
